from pydantic import BaseModel
from typing import List


class Params(BaseModel):
    description: str
    values: List[str]
    default: List[str]


def set_cmd_params(cmd_params: dict):
    """
    设置命令参数,一般用于部署时设置不同环境，不同平台等
    :param cmd_params:
        {
        "runtime": {"description": "请输入运行环境", "values": ["test", "dev", "prod"], "default": ["test"]}
        }
    :return:
    """
    for p in cmd_params.values():
        Params(**p)
    indent = 8
    for k, v in cmd_params.items():
        description = v.get("description")
        values = v.get('values', [])
        default = v.get("default", [])
        ########################################
        print(description)
        for idx, vl in enumerate(values):
            print(" " * indent + vl.ljust(4, " "), ": ", idx)
        ########################################
        tip = " " * indent + f"default params: {''.join(default)} ," \
                             f"please input number(example 1,2)  -->  ".ljust(40, " ")
        value = input(tip).strip()
        if not value:
            v["default"] = default
        else:
            try:
                value = set(map(int, value.split(",")))
            except:
                raise Exception("please give int type")
            assert value.issubset(set(range(len(values)))), f"please input number 0-{len(values)}"
            default = []
            for idx in value:
                default.append(values[idx])
        v["default"] = default
        ########################################
        print(" " * (indent - 1), "finally params: ", "/".join(default))
    return cmd_params
